// pages/Purchasing/houseprice/houseprice.js
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    housepriceText: '',
    housepricerate: '',
    housepriceyear: '',
    mcinmate: '',
    purchasing: '',
    statisticsArray: [],
    showPurchasing: false,
    showMoyouth: false,
    Moyouth: '',
    houseType: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.calculatehouseprice()

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },/*計算方式*/
  calculatehouseprice: function () {
    var _showPurchasing = false;
    var _showMoyouth = false;
    var resultArray = new Array();
    var khouseMCinmate = wx.getStorageSync('khouseMCinmate')
    var khousePurchasing = wx.getStorageSync('khousePurchasing')
    var khousepriceText = parseFloat(wx.getStorageSync('khousepriceText')) * 10000//樓價
    var khousepricerate = parseFloat(wx.getStorageSync('khousepricerate'))
    var khousepriceyear = parseInt(wx.getStorageSync('khousepriceyear'))
    var Moyouth = wx.getStorageSync('khouseMoyouth')
    var calculateType = wx.getStorageSync('khousecalculateType')
    var repaymenmonth = khousepriceyear * 12 //期數
    var monthrate = khousepricerate / 100 / 12
    var _mcinmate = ''; var _purchasing = ''; var _Moyouth = '否'; var _calculateType = ''
    if (khouseMCinmate == 'MC1') {
      _mcinmate = '是'

    }
    if (khouseMCinmate == 'MC2') {
      _mcinmate = '否';
      _showPurchasing = true;
      _showMoyouth = true;
    }

    if (khousePurchasing == 'first')
      _purchasing = '是'
    if (khousePurchasing == 'nofirst') {
      _purchasing = '否';
      _showMoyouth = true;
    }
    if (Moyouth == 'youth')
      _Moyouth = '是'
    if (calculateType == 'Real')
      _calculateType = '否'
    if (calculateType == 'Stage')
      _calculateType = '是'
    for (var i = 1; i <= 9; i++) {
      var pv = khousepriceText * (i / 10);
      var roundpv = ''
      if (pv >= 10000)
        roundpv = util.formatCurrency(Math.ceil((pv / 10000).toFixed(1))) + '萬(' + (i * 10) + '%)'
      else
        roundpv = util.formatCurrency(Math.ceil((pv).toFixed(1))) + '元(' + (i * 10) + '%)'

      var first = khousepriceText - pv;//(楼价-按揭)首期
     
      var roundfirst = ''
      if (first >= 10000)
        roundfirst = util.formatCurrency((first / 10000).toFixed(0)) + '萬'// util.formatCurrency(Math.ceil((first / 10000))) + '萬'
      else
        roundfirst = util.formatCurrency(first.toFixed(0)) + '元' //util.formatCurrency(Math.ceil((first))) + '元'

      // loan = khousepriceText*(i/10) //貸款額=樓價*10%
      var instarll = (pv * monthrate * Math.pow(1 + monthrate, repaymenmonth)) / (Math.pow(1 + monthrate, repaymenmonth) - 1)
      var roundinstarll = util.formatCurrency(Math.ceil((instarll))) + '元'

     // var per = util.JudgementHoust(pv, khouseMCinmate, khousePurchasing, Moyouth, calculateType) * 10; //获取用户最大区间
     // console.log(per)
      //if (i <= per) {
       // var mortgage = util.GetMaxLoan(pv, khouseMCinmate, khousePurchasing, Moyouth, calculateType);//返回标准金额
        //var s=0;
       
        var s = { first, roundfirst, roundpv, roundinstarll, pv, s }
        resultArray.push(s)

    //  }

    }
    console.log(resultArray)
    var temp;
    for (var i = 0; i < resultArray.length; i++) {
      for (var j = i + 1; j < resultArray.length; j++) {
        if (resultArray[i].first > resultArray[j].first) {
          temp = resultArray[i];
          resultArray[i] = resultArray[j];
          resultArray[j] = temp;
        }
      }
    }

    this.setData({

      housepriceText: '$' + util.formatCurrency(khousepriceText),
      housepricerate: khousepricerate + '%',
      housepriceyear: khousepriceyear + '年(' + repaymenmonth + '期)',//還款年期
      mcinmate: _mcinmate,
      purchasing: _purchasing,
      statisticsArray: resultArray,
      showPurchasing: _showPurchasing,
      Moyouth: _Moyouth,
      houseType: _calculateType,
      showMoyouth: _showMoyouth,
    })

  }
})